package com.army.study.ui.listcomponent;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.List;

public abstract class ListComponentAdapter<T> extends BaseItemProvider {

    LayoutScatter layoutScatter;
    private Context mContext;
    private List<T> mListBeans;
    private int mXmlId;

    public ListComponentAdapter(Context context, List<T> list, int xmlId) {
        this.mContext = context;
        this.mListBeans = list;
        this.mXmlId = xmlId;
        layoutScatter = LayoutScatter.getInstance(mContext);
    }

    public abstract void onBindViewHolder(CommentViewHolder commentViewHolder, T item, int position);

    @Override
    public int getCount() {
        return mListBeans.size();
    }

    @Override
    public T getItem(int i) {
        return mListBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        CommentViewHolder commentViewHolder = CommentViewHolder.getCommentViewHolder(mContext, component, mXmlId);
        T t = mListBeans.get(i);
        onBindViewHolder(commentViewHolder, t, i);
        commentViewHolder.getConvertView().setClickedListener(component1 -> onItemClick(component, t, i));

        return commentViewHolder.getConvertView();
    }

    public void onItemClick(Component component, T t, int i) {
    }
}
